/*
 * Copyright (C) 2022, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: iaom <zhangpengfei@kylinos.cn>
 *
 */
#include "monitor.h"
#include <QMetaEnum>
#include "file-indexer-config.h"
using namespace UkuiSearch;
Monitor::Monitor(IndexScheduler *scheduler, QObject *parent)
    : QObject(parent),
      m_scheduler(scheduler),
      m_basicDatabase(DataBaseType::Basic),
      m_contentDatabase(DataBaseType::Content)
{
    connect(scheduler, &IndexScheduler::stateChange, this, &Monitor::onIndexStateChanged);
    connect(scheduler, &IndexScheduler::process, this, &Monitor::processUpdate);
    connect(scheduler, &IndexScheduler::basicIndexDone, this, &Monitor::basicIndexDone);
    connect(scheduler, &IndexScheduler::contentIndexDone, this, &Monitor::contentIndexDone);
}

QStringList Monitor::getCurrentIndexPaths()
{
    return FileIndexerConfig::getInstance()->currentIndexableDir();
}

QString Monitor::getIndexState()
{
    QMetaEnum metaEnum = QMetaEnum::fromType<IndexScheduler::IndexerState>();
    return QString::fromLocal8Bit(metaEnum.valueToKey(m_scheduler->getIndexState()));
}

uint Monitor::getBasicIndexSize()
{
    return m_basicIndexSize;
}

uint Monitor::getContentIndexSize()
{
    return m_contentIndexSize;
}

uint Monitor::getOCRIndexSize()
{
    return m_ocrIndexSize;
}

uint Monitor::getBasicIndexProgress()
{
    return m_basicIndexProgress;
}

uint Monitor::getContentIndexProgress()
{
    return m_contentIndexProgress;
}

uint Monitor::getOCRIndexProgress()
{
    return m_ocrIndexProgress;
}

uint Monitor::getBasicIndexDocNum()
{
    return m_basicDatabase.getIndexDocCount();
}

uint Monitor::getContentIndexDocNum()
{
    return m_contentDatabase.getIndexDocCount();
}

void Monitor::onIndexStateChanged(IndexScheduler::IndexerState state)
{
    if(state == IndexScheduler::IndexerState::Idle) {
        Q_EMIT basicIndexDocNumUpdate(m_basicDatabase.getIndexDocCount());
        Q_EMIT contentIndexDocNumUpdate(m_contentDatabase.getIndexDocCount());
    }
    QMetaEnum metaEnum = QMetaEnum::fromType<IndexScheduler::IndexerState>();
    Q_EMIT indexStateChanged(QString::fromLocal8Bit(metaEnum.valueToKey(state)));
}

void Monitor::processUpdate(IndexType type, uint all, uint finished)
{
    switch (type) {
    case IndexType::Basic:
        m_basicIndexSize = all;
        Q_EMIT basicIndexSizeChange(m_basicIndexSize);
        m_basicIndexProgress = finished;
        Q_EMIT basicIndexProgressUpdate(m_basicIndexProgress);
        break;
    case IndexType::Contents:
        m_contentIndexSize = all;
        Q_EMIT contentIndexSizeChange(m_contentIndexSize);
        m_contentIndexProgress = finished;
        Q_EMIT contentIndexProgressUpdate(m_contentIndexProgress);
        break;
    case IndexType::OCR:
        m_ocrIndexSize = all;
        Q_EMIT ocrIndexSizeChange(m_ocrIndexSize);
        m_contentIndexProgress = finished;
        Q_EMIT ocrIndexProgressUpdate(m_contentIndexProgress);
        break;
    default:
        break;
    }
}
